package com.itboye.hutouben.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.bean.LunBoBean;
import com.itboye.hutouben.presenter.LunBoPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.XImageLoader;
import com.itboye.hutouben.volley.ResultEntity;
import com.itboye.hutouben.web.WebActivity;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * 轮播图
 */
public class ImagePagerAdapter extends PagerAdapter implements Observer{

	private ArrayList<LunBoBean> ids;
	private Context context;
	private LayoutInflater inflater;

	LunBoPresenter lunBoPresenter;
	public ImagePagerAdapter(Context context,ArrayList<LunBoBean> url){
		this.ids=url;
		this.context=context;
		inflater = LayoutInflater.from(context);
		lunBoPresenter=new LunBoPresenter(this);
		
	}
	
	@Override
	public int getCount() {
		return ids.size();
	}
	
    @Override  
    public boolean isViewFromObject(View arg0, Object arg1) {  
        return arg0 == arg1;  
    }  
  
    @Override  
    public void destroyItem(ViewGroup container, int position, Object object) {  
        container.removeView((View) object);  
    }

    @Override  
    public Object instantiateItem(ViewGroup container, final int position) {
    	View view=LayoutInflater.from(context).inflate(R.layout.ad_imageview,null);
		ImageView image= (ImageView) view.findViewById(R.id.image);
		try {
			XImageLoader.load(Const.imgurl+ids.get(position).getImg(),image);
		}catch (Exception e){

		}
		System.out.println("轮播图片"+Const.imgurl+ids.get(position).getImg());
		image.setScaleType(ImageView.ScaleType.FIT_XY);

		image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!ids.get(position).getUrl().equals("")){
					lunBoPresenter.lunBoTongJi(ids.get(position).getId());
					Intent intent = new Intent(context, WebActivity.class);
					intent.putExtra("url", ids.get(position).getUrl());
					intent.putExtra("title", ids.get(position).getTitle().equals("")?"虎头奔":ids.get(position).getTitle());
					context.startActivity(intent);
				}
			}
		});
    	container.addView(view);


    	return view;
    }

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == LunBoPresenter.lunbo_tongji_success) {
			}
			if (entity.getEventType() == LunBoPresenter.lunbo_tongji_fail) {
			}
		}

	}
	private String EVENT_TYPE_UNKNOWN = "_unknown";

	public ResultEntity handlerError(Object data) {
		ResultEntity result = (ResultEntity) data;
		if (result == null) {
			result = new ResultEntity("-1", "数据格式错误!~", data);
			return result;
		}

		if (result.hasError()) {
			ByAlert.alert(result.getMsg());
			result.setEventType(EVENT_TYPE_UNKNOWN);
			return result;
		}

		return result;
	}
}
